package com.huiminpay.paymentagent.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayAcquireQueryRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.paymentagent.api.conf.AliConfigParam;
import com.huiminpay.paymentagent.api.dto.AlipayBean;
import com.huiminpay.paymentagent.api.dto.AlipayTradeQueryDTO;
import com.huiminpay.paymentagent.api.service.IAlipaytAgentService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;

/**
 * @ClassName : AlipaytAgentServiceImpl
 * @Author : 阿泽
 * @Date: 2021/8/15 10:42
 * @Description :
 */
@Service
public class AlipaytAgentServiceImpl implements IAlipaytAgentService {
    @Override
    public String doAlipayWebApi(AliConfigParam aliConfigParam, AlipayBean alipayBean) throws BusinessException {
        /**
         * 网关地址
         */
        String serverUrl = aliConfigParam.getUrl();
        /**
         * 应用ID
         */
        String appId = aliConfigParam.getAppId();
        /**
         * 应用私钥
         */
        String privateKey = aliConfigParam.getRsaPrivateKey();
        String format = aliConfigParam.getFormat();
        String charset = aliConfigParam.getCharest();
        /**
         * 支付宝公钥
         */
        String alipayPublicKey = aliConfigParam.getAlipayPublicKey();
        String signType = aliConfigParam.getSigntype();

        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,appId,privateKey,format,charset,alipayPublicKey,signType);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        if (StringUtils.isNotEmpty(aliConfigParam.getNotifyUrl())){
            request.setNotifyUrl(aliConfigParam.getNotifyUrl()); //设置异步通知地址
        }
        AlipayTradeWapPayModel alipayTradeWapPayModel = new AlipayTradeWapPayModel();
        //下面是业务参数
        /**
         * 订单标题
         */
        alipayTradeWapPayModel.setSubject(alipayBean.getSubject());
        /**
         * 订单号
         */
        alipayTradeWapPayModel.setOutTradeNo(alipayBean.getOutTradeNo());
        /**
         * 订单金额
         */
        alipayTradeWapPayModel.setTotalAmount(alipayBean.getTotalAmount());
        alipayTradeWapPayModel.setQuitUrl("-");
        alipayTradeWapPayModel.setProductCode("QUICK_WAP_PAY");

//        request.setBizContent("");
        request.setBizModel(alipayTradeWapPayModel);
        try {
            AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
            if (response.isSuccess()){
                return response.getBody();
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            throw new BusinessException(CommonErrorCode.UNKOWN);
        }
        throw new BusinessException(CommonErrorCode.UNKOWN);
    }

    @Override
    public AlipayTradeQueryDTO queryAlipayOrderByTradeNo(AliConfigParam aliConfigParam, String outTradeNo) throws BusinessException {
        /**
         * 网关地址
         */
        String serverUrl = aliConfigParam.getUrl();
        /**
         * 应用ID
         */
        String appId = aliConfigParam.getAppId();
        /**
         * 应用私钥
         */
        String privateKey = aliConfigParam.getRsaPrivateKey();
        String format = aliConfigParam.getFormat();
        String charset = aliConfigParam.getCharest();
        /**
         * 支付宝公钥
         */
        String alipayPublicKey = aliConfigParam.getAlipayPublicKey();
        String signType = aliConfigParam.getSigntype();

        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,appId,privateKey,format,charset,alipayPublicKey,signType);

        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(outTradeNo);
        request.setBizModel(model);
        try {
            AlipayTradeQueryResponse execute = alipayClient.execute(request);
            if (execute.isSuccess()){
                AlipayTradeQueryDTO alipayTradeQueryDTO = new AlipayTradeQueryDTO();
                alipayTradeQueryDTO.setTradeNo(execute.getTradeNo());
                alipayTradeQueryDTO.setOutTradeNo(execute.getOutTradeNo());
                alipayTradeQueryDTO.setTradeStatus(execute.getTradeStatus());
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        throw new BusinessException(CommonErrorCode.UNKOWN);
    }
}
